package com.ncloud.chnl.mapper;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ncloud.chnl.entity.CustAreaUser;  
@Mapper
public interface CustAreaUserMapper{  
  
    @Select(value = {"select * from bs_cust_area_user_rel where area_id = #{areaId} and user_id = #{userId} and state ='1'" })
	@Results({ @Result(column = "rel_inst_id", property = "relInstId"),@Result(column = "cust_id", property = "custId"),
			@Result(column = "area_id", property = "areaId"), @Result(column = "user_id", property = "userId"),
			@Result(column = "state", property = "state"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),@Result(column = "is_manager", property = "isManager"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark")})
    public CustAreaUser getCustAreaUser(@Param("areaId")Long areaId,@Param("userId")Long userId);
  
    @Select(value = {"select a.*,b.name as userName from bs_cust_area_user_rel a,sys_user b where a.area_id = #{areaId} and a.state ='1'"
    		+ " and a.user_id = b.id and b.state<>'0'" })  
	@Results({ @Result(javaType=String.class, column = "rel_inst_id", property = "relInstId"),
			@Result(javaType=String.class, column = "cust_id", property = "custId"),
			@Result(javaType=String.class, column = "area_id", property = "areaId"),  
			@Result(javaType=String.class, column = "user_id", property = "userId"),
			@Result(column = "state", property = "state"), @Result(column = "create_time", property = "createTime"),
			@Result(column = "create_user", property = "createUser"),@Result(column = "is_manager", property = "isManager"),
			@Result(column = "update_time", property = "updateTime"),
			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark")}) 
    public List<Map<String, Object>> getAllCustAreaUsers(Long areaId);


    @Insert(value = ("insert into bs_cust_area_user_rel(rel_inst_id,cust_id,area_id,user_id,is_manager,state,create_time,create_user,update_time,update_user,remark)"
    		+ " values (#{relInstId},#{custId},#{areaId},#{userId},#{isManager},#{state},#{createTime},#{createUser},#{updateTime},#{updateUser},#{remark})"))
    public int insertCustAreaUser(CustAreaUser custAreaUser);
    
    @Update(value = ("update bs_cust_area_user_rel set state = #{state},update_time = now(),"
    		+ " update_user = #{updateUser},remark = #{remark} where rel_inst_id = #{relInstId}"))
    public int updateCustAreaUser(CustAreaUser custAreaUser);

    @Select(value = {"select * from bs_cust_area_user_rel where user_id = #{userId} and state ='1'" })
   	@Results({ @Result(column = "rel_inst_id", property = "relInstId"),@Result(column = "cust_id", property = "custId"),
   			@Result(column = "area_id", property = "areaId"), @Result(column = "user_id", property = "userId"),
   			@Result(column = "is_manager", property = "isManager"),
   			@Result(column = "state", property = "state"), @Result(column = "create_time", property = "createTime"),
   			@Result(column = "create_user", property = "createUser"),
   			@Result(column = "update_time", property = "updateTime"),
   			@Result(column = "update_user", property = "updateUser"), @Result(column = "remark", property = "remark")})
	public CustAreaUser getCustAreaUserByUserId(Long userId);

    @Update("update bs_cust_area_user_rel set state = '0',update_time = now(), update_user = #{1} where area_id = #{0}")
	public void deleteCustAreaUsers(Long areaId, Long userId);
    
} 